package com.vone.view;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.vone.util.ImageUtil;
import com.vone.util.SizeCalculationUtil;

/**
 * 标题面板--LOGO、设置、用户........
 * @author wd
 *
 */
public class TitleJPanelView extends JPanel{
	private static final long serialVersionUID = 1L;
	
	private int moveX,moveY;//移动窗口的全局坐标
	//LOGO图片
	private JLabel logo = null;
	//系统名称
	private JLabel systemName = null;
	//最小化
	private JLabel minimize = null;
	//关闭
	private JLabel close = null;
	
	//计算工具
	private SizeCalculationUtil sizeUtil = new SizeCalculationUtil();
	public TitleJPanelView(final JFrame mainJFrame){
		this.setLayout(null);
		this.setBackground(new Color(39,157,227));
		
		logo = new JLabel(){
			private static final long serialVersionUID = 1L;
			public void paint(Graphics g) {
				super.paint(g);
				Image image = null;
				image = ImageUtil.getLogo();
				if (image != null)
					g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
			}
		};
		
		systemName = new JLabel("大赛报名管理（信息导出）系统",JLabel.LEFT);
		systemName.setFont(new Font("微软雅黑", Font.BOLD, 18));
		systemName.setForeground(Color.white);
		
		//窗口状态栏
		minimize = new JLabel("─",JLabel.CENTER);
		minimize.setFont(new Font("微软雅黑",Font.PLAIN,sizeUtil.getFontSize(20)));
		minimize.setForeground(Color.white);
		//minimize.setBorder(BorderFactory.createLineBorder(Color.white));
		
		close = new JLabel("╳",JLabel.CENTER);
		close.setFont(new Font("微软雅黑",Font.PLAIN,sizeUtil.getFontSize(20)));
		close.setForeground(Color.white);
		//close.setBorder(BorderFactory.createLineBorder(Color.white));
		
		
		logo.setBounds(sizeUtil.getX(10), sizeUtil.getY(14), sizeUtil.getW(65), sizeUtil.getH(20));
		systemName.setBounds(sizeUtil.getX(80), sizeUtil.getY(4), sizeUtil.getW(300), sizeUtil.getH(40));
		minimize.setBounds(sizeUtil.getX(810), sizeUtil.getY(4), sizeUtil.getW(40), sizeUtil.getH(40));
		close.setBounds(sizeUtil.getX(850), sizeUtil.getY(4), sizeUtil.getW(40), sizeUtil.getH(40));
		
		this.add(logo);
		this.add(systemName);
		this.add(minimize);
		this.add(close);
		
		//绑定移动窗口事件
		this.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                    moveX = e.getX();
                    moveY = e.getY();
            }
		});
		this.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                    int left = mainJFrame.getLocation().x;
                    int top = mainJFrame.getLocation().y;
                    mainJFrame.setLocation(left + e.getX() - moveX, top + e.getY() - moveY);
            }  
		});
		
		//最小化(绑定事件)
		minimize.addMouseListener(new MouseAdapter(){
			//点击事件
			public void mouseClicked(MouseEvent e){
				mainJFrame.setExtendedState(JFrame.ICONIFIED);//窗口最小化
		    }
			//鼠标进入事件
			public void mouseEntered(MouseEvent e) {
				minimize.setOpaque(true);
				minimize.setBackground(new Color(221,80,68));
				minimize.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标变成手型
			}
			//鼠标移出事件
			public void mouseExited(MouseEvent e) {
				minimize.setOpaque(false);
				minimize.setBackground(new Color(0,104,183));
				minimize.setCursor(Cursor.getDefaultCursor());//鼠标变回默认
			}
		});
		
		//x退出(绑定事件)
		close.addMouseListener(new MouseAdapter(){
			//点击事件
			public void mousePressed(MouseEvent e){
				//弹出确认框,返回0或者1,0=true(yes),1=false(no)
				int n = JOptionPane.showConfirmDialog(null, "你确认要退出系统吗？", "系统信息", JOptionPane.YES_NO_OPTION);
		    	if(n==0){
		    		System.exit(0);
		    	}
		    }
			//鼠标进入事件
			public void mouseEntered(MouseEvent e) {
				close.setOpaque(true);
				close.setBackground(new Color(221,80,68));
				close.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标变成手型
			}
			//鼠标移出事件
			public void mouseExited(MouseEvent e) {
				close.setOpaque(false);
				close.setBackground(new Color(0,104,183));
				close.setCursor(Cursor.getDefaultCursor());//鼠标变回默认
			}
		});
	}
}
